package org.pearl.study.transmission.demo;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
import org.springframework.security.config.annotation.web.configurers.AbstractConfigAttributeRequestMatcherRegistry;
import org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.util.Assert;

final class PermitAllSupport {
    private PermitAllSupport() {
    }

    static void permitAll(HttpSecurityBuilder<? extends HttpSecurityBuilder<?>> http, String... urls) {
        String[] var2 = urls;
        int var3 = urls.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            String url = var2[var4];
            if (url != null) {
                permitAll(http, new org.springframework.security.config.annotation.web.configurers.PermitAllSupport.ExactUrlRequestMatcher(url));
            }
        }

    }

    static void permitAll(HttpSecurityBuilder<? extends HttpSecurityBuilder<?>> http, RequestMatcher... requestMatchers) {
        ExpressionUrlAuthorizationConfigurer<?> configurer = (ExpressionUrlAuthorizationConfigurer)http.getConfigurer(ExpressionUrlAuthorizationConfigurer.class);
        AuthorizeHttpRequestsConfigurer<?> httpConfigurer = (AuthorizeHttpRequestsConfigurer)http.getConfigurer(AuthorizeHttpRequestsConfigurer.class);
        boolean oneConfigurerPresent = configurer == null ^ httpConfigurer == null;
        Assert.state(oneConfigurerPresent, "permitAll only works with either HttpSecurity.authorizeRequests() or HttpSecurity.authorizeHttpRequests(). Please define one or the other but not both.");
        RequestMatcher[] var5 = requestMatchers;
        int var6 = requestMatchers.length;

        for(int var7 = 0; var7 < var6; ++var7) {
            RequestMatcher matcher = var5[var7];
            if (matcher != null) {
                if (configurer != null) {
                    configurer.getRegistry().addMapping(0, new AbstractConfigAttributeRequestMatcherRegistry.UrlMapping(matcher, SecurityConfig.createList(new String[]{"permitAll"})));
                } else {
                    httpConfigurer.addFirst(matcher, AuthorizeHttpRequestsConfigurer.permitAllAuthorizationManager);
                }
            }
        }

    }

    private static final class ExactUrlRequestMatcher implements RequestMatcher {
        private String processUrl;

        private ExactUrlRequestMatcher(String processUrl) {
            this.processUrl = processUrl;
        }

        public boolean matches(HttpServletRequest request) {
            String uri = request.getRequestURI();
            String query = request.getQueryString();
            if (query != null) {
                uri = uri + "?" + query;
            }

            if ("".equals(request.getContextPath())) {
                return uri.equals(this.processUrl);
            } else {
                String var10001 = request.getContextPath();
                return uri.equals(var10001 + this.processUrl);
            }
        }

        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append("ExactUrl [processUrl='").append(this.processUrl).append("']");
            return sb.toString();
        }
    }
}
